package com.wingscode.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wingscode.common.annotation.SysLog;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.common.validator.ValidatorUtils;
import com.wingscode.common.validator.group.AddGroup;
import com.wingscode.common.validator.group.UpdateGroup;
import com.wingscode.modules.sys.entity.DictionaryKeyEntity;
import com.wingscode.modules.sys.entity.DictionaryValueEntity;
import com.wingscode.modules.sys.service.DictionaryKeyService;
import com.wingscode.modules.sys.service.DictionaryValueService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典
 *
 * @author yan
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController extends AbstractController {
	@Autowired
	private DictionaryKeyService dictionaryKeyService;
	@Autowired
	private DictionaryValueService dictionaryValueService;

	/**
	 * 所有字典key列表
	 */
	@GetMapping("/keyList")
	public R keyList(@RequestParam Map<String, Object> params){
		PageUtils page = dictionaryKeyService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 所有字典value列表
	 */
	@GetMapping("/valueList")
	public R valueList(@RequestParam Map<String, Object> params){
		PageUtils page = dictionaryValueService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 根据type获得分页列表信息
	 */
	@GetMapping("/listByType")
	public R listByType(@RequestParam Map<String, Object> params){
		String type = "";
		if (!params.containsKey("type"))
			return R.error("type为空");

		type = (String) params.get("type");
		DictionaryKeyEntity keyEntity = dictionaryKeyService.getOne(
				new QueryWrapper<DictionaryKeyEntity>()
						.eq("type", type)
		);

		if (keyEntity != null)
			params.put("pid", keyEntity.getId());

		PageUtils page = dictionaryValueService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 字典key信息
	 */
	@GetMapping("/keyInfo/{id}")
	public R keyInfo(@PathVariable("id") Long id){
		DictionaryKeyEntity entity = dictionaryKeyService.getById(id);

		return R.ok().put("keyInfo", entity);
	}

	/**
	 * 字典value信息
	 */
	@GetMapping("/valueInfo/{id}")
	public R valueInfo(@PathVariable("id") Long id){
		DictionaryValueEntity entity = dictionaryValueService.getById(id);

        return R.ok().put("valueInfo", entity);
	}

	/**
	 * 保存字典key
	 */
	@PostMapping("/keySave")
	public R keySave(@RequestBody DictionaryKeyEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);

		List<DictionaryKeyEntity> list = this.dictionaryKeyService.list(
				new QueryWrapper<DictionaryKeyEntity>()
						.eq("name", entity.getName())
						.or()
						.eq("type", entity.getType())
		);

		if (list != null && list.size() > 0)
			return R.error("当前字典key已存在");

		dictionaryKeyService.save(entity);

		return R.ok();
	}

	/**
	 * 保存字典value
	 */
	@PostMapping("/valueSave")
	public R valueSave(@RequestBody DictionaryValueEntity entity){
		if (entity.getPid() == null && entity.getType() == null && entity.getType().isEmpty())
			return R.error("pid或type为空");
		if (entity.getPid() == null && entity.getType() != null) {
			DictionaryKeyEntity keyEntity = dictionaryKeyService.getOne(
					new QueryWrapper<DictionaryKeyEntity>()
							.eq("type", entity.getType())
			);
			if (keyEntity != null)
				entity.setPid(keyEntity.getId());
			else
				return R.error("新增失败");
		}

		// 检查字典value是否重复
		List<DictionaryValueEntity> list = this.dictionaryValueService.list(
				new QueryWrapper<DictionaryValueEntity>()
						.eq("name", entity.getName())
						.eq("pid", entity.getPid())
		);

		if (list != null && list.size() > 0)
			return R.error("当前字典value已存在");

		entity.setState(1);
		ValidatorUtils.validateEntity(entity, AddGroup.class);

		dictionaryValueService.save(entity);

		return R.ok();
	}

	/**
	 * 修改字典key
	 */
	@PostMapping("/keyUpdate")
	public R keyUpdate(@RequestBody DictionaryKeyEntity entity){
		ValidatorUtils.validateEntity(entity, UpdateGroup.class);

		dictionaryKeyService.updateById(entity);

		return R.ok();
	}

	/**
	 * 修改字典value
	 */
	@PostMapping("/valueUpdate")
	public R valueUpdate(@RequestBody DictionaryValueEntity entity){
		ValidatorUtils.validateEntity(entity, UpdateGroup.class);

		// 检查字典value是否重复
		List<DictionaryValueEntity> list = this.dictionaryValueService.list(
				new QueryWrapper<DictionaryValueEntity>()
						.eq("name", entity.getName())
						.eq("pid", entity.getPid())
		);

		if (list != null && list.size() > 0)
			return R.error("当前字典value已存在");

		dictionaryValueService.updateById(entity);

		return R.ok();
	}

	/**
	 * 删除字典key
	 */
	@PostMapping("/keyDelete")
	public R keyDelete(@RequestBody Long[] Ids){

		dictionaryKeyService.deleteBatch(Ids);

		return R.ok();
	}

	/**
	 * 删除字典value
	 */
	@PostMapping("/valueDelete")
	public R valueDelete(@RequestBody Long[] Ids){
		if (Ids != null && Ids.length > 0) {
			for (Long id : Ids) {
				DictionaryValueEntity valueEntity = dictionaryValueService.getById(id.intValue());
				if (valueEntity != null && valueEntity.getState() == 1)
					return R.error("当前数据正在使用，无法删除");
			}
		}

		dictionaryValueService.deleteBatch(Ids);

		return R.ok();
	}

	/**
	 * 字典key list
	 */
	@GetMapping("/allKey")
	public R allKey(){
		List<DictionaryKeyEntity> list = dictionaryKeyService.getList();

		return R.ok(200, list);
	}
	
	/**
	 * 字典value list
	 */
	@PostMapping("/allValue")
	public R allValue(@RequestBody Map<String, Object> params){
		List<DictionaryValueEntity> list = dictionaryValueService.getList(params);

		return R.ok().put("list", list);
	}
}
